﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using chihuopig.Web.Framework;
using chihuopig.IService;
using chihuopig.Web.verfiy;
using chihuopig.Service;
using chihuopig.DTO;

namespace chihuopig.Web.Admin.Controllers
{
    public class adminroleController : AdminWattantyController
    {
        IHttpContextAccessor _iHttpContextAccessor;
        IRoleService _iRoleService;
        IAuthorization _iAuthorization;
        public adminroleController (
            IHttpContextAccessor iHttpContextAccessor,
            IRoleService iRoleService,
            IAuthorization iAuthorization


            ) : base(iHttpContextAccessor) 
        {
            _iHttpContextAccessor = iHttpContextAccessor;
            _iRoleService = iRoleService;
            _iAuthorization = iAuthorization;
        }

        public async Task<IActionResult> roleadd()
        {
           List<DTO.RoleModel> list= await _iAuthorization.GetTable();
           return View(list);
        }

        [HttpPost]
        public async Task<IActionResult> roleadd(roleaddVefiy model)
        {
            if (ModelState.IsValid)
            {

                try
                {
                     DBService.Sqlclient.Ado.BeginTran();
                     await _iAuthorization.RoleAdd(new DTO.roleaddVefiyDTO() { jurisd=model.jurisd, RoleName=model.RoleName });
                     DBService.Sqlclient.Ado.CommitTran();
                    return Json(new JsonMsg() { succeed = true, msg = "添加成功" });
                }
                catch(Exception ex)
                {
                    DBService.Sqlclient.Ado.RollbackTran();
                    return Json(new JsonMsg() { succeed = false, msg = "添加失败" });
                }

            }
           
            return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        [HttpPost]
        public async Task<IActionResult> delAll(long[] id)
        {
            var resust = await _iRoleService.delAll(id);
            if (resust > 0)
            {
                return Json(new { succeed = true, msg = "删除成功" });
            }
            return Json(new { succeed = false, msg = "删除失败" });
        }

        public async Task<IActionResult> roleEdit(long Id)
        {
            RoleModelEdit model = new RoleModelEdit();
            var finmodel=await _iAuthorization.GetTable();
            var RoleModeldto = await _iAuthorization.FindRoleModelDTO(Id);
            ViewBag.RoleId = Id;
            model.rolm = finmodel;
            model.roleDto = RoleModeldto;
            return View(model);
        }
        [HttpPost]
        public async Task<IActionResult> roleEdit(roleaddVefiyDTOs model)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    DBService.Sqlclient.Ado.BeginTran();
                    await _iAuthorization.RoleAdd(new DTO.roleaddVefiyDTO() { jurisd = model.jurisd, RoleName = model.RoleName, RoleId=model.RoleId });
                    DBService.Sqlclient.Ado.CommitTran();
                    return Json(new JsonMsg() { succeed = true, msg = "添加成功" });
                }
                catch (Exception ex)
                {
                    DBService.Sqlclient.Ado.RollbackTran();
                    return Json(new JsonMsg() { succeed = false, msg = "添加失败" });
                }

            }

            return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        public async Task<IActionResult> List(int Pageindex = 1, int PageSize =10)
        {
            var list = await _iRoleService.GetPage(Pageindex, PageSize);
            var total = await _iRoleService.GetPageTotal(PageSize);
            ViewBag.Pageindex = Pageindex;
            ViewBag.Start = Pageindex + 5 > total ? 1 : Pageindex + 5;
            ViewBag.GetPageTotal = total + 5 > total ? total : total + 5;
            return View(list);
        }
    }
}